package com.hrbxlh.virtualization.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 辅助获取登录用户信息
 *
 * @author cheer
 */
public class SessionUtil {

    private SessionUtil() {
    }

    /**
     * 查询当前登录用户Id
     *
     * @return 用户Id
     */
    @SuppressWarnings("unchecked")
    public static String getStuorteaId() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Map<String, Object> userMsg = (Map<String, Object>) request.getSession().getAttribute("userMsg");
        return (String) userMsg.get("stuorteaId");
    }

    /**
     * 查询当前登录角色Id
     *
     * @return 角色Id
     */
    @SuppressWarnings("unchecked")
    public static Integer getRoleId() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Map<String, Object> userMsg = (Map<String, Object>) request.getSession().getAttribute("userMsg");
        return (Integer) userMsg.get("roleId");
    }

    /**
     * 查询当前登录用户名称
     *
     * @return 用户名称
     */
    @SuppressWarnings("unchecked")
    public static String getUsername() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Map<String, Object> userMsg = (Map<String, Object>) request.getSession().getAttribute("userMsg");
        return (String) userMsg.get("userName");
    }
}
